﻿/*
 * Created by SharpDevelop.
 * User: Jack Brinegar
 * Date: 1/29/2013
 * Time: 7:24 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace AdminDesktop.Controls
{
	/// <summary>
	/// Description of ucServiceDays.
	/// </summary>
	public partial class ucServiceDays : UserControl
	{
		//public ButternutSkiPatrol.DataFile _data;
		
		public ucServiceDays()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			LoadData();
			
			//this.Dock = DockStyle.Fill;
			//this.Anchor = AnchorStyles.Top;
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			this.Dock = DockStyle.Fill;
			
			
		}
				
		public void LoadData()
		{
			
			
			this.comboBox1.Items.Clear();
			for(int i = 0; i < DataEngine.APP_CONFIG.ServiceTypes.ServiceTypeList.Count;i++)
			{
				comboBox1.Items.Add(DataEngine.APP_CONFIG.ServiceTypes.ServiceTypeList[i].Name);
				//this.dgrdServiceDays.Columns[1]
			}
			
			this.lvwPatrollers.Items.Clear();
			this.lvwPatrollers.SmallImageList=this.imgList;
			this.lvwServicePatrollers.SmallImageList=this.imgList;
			if(DataEngine.dataFile.ServiceDayRecord!="")
			{
				try
				{
					this.lblLastDay.Text = this.lblLastDay.Tag + " " + DateTime.Parse(DataEngine.dataFile.ServiceDayRecord).ToShortDateString();
					this.monthCalendar1.SetDate(DateTime.Parse(DataEngine.dataFile.ServiceDayRecord).AddDays(1));
				}
				catch
				{
					
				}
			}
			
			
			if(DataEngine.dataFile.roster!=null)
			{
				for(int i = 0; i < DataEngine.dataFile.roster.Patrollers.Count;i++)
				{
					ListViewItem myItem = this.lvwPatrollers.Items.Add(Utility.TitleCaseString(DataEngine.dataFile.roster.Patrollers[i].LastName) + ", " + Utility.TitleCaseString(DataEngine.dataFile.roster.Patrollers[i].FirstName) );
					myItem.Tag = DataEngine.dataFile.roster.Patrollers[i];
					switch(DataEngine.dataFile.roster.Patrollers[i].Level)
					{
						case "Candidate":
							myItem.ImageIndex=0;
							break;
						case "Non S&T Patroller":
							myItem.ImageIndex=1;
							break;
						case "S&T Patroller":
							myItem.ImageIndex=2;
							break;
						case "Senior":
							myItem.ImageIndex=3;
							break;
						case "Hill Captain":
							myItem.ImageIndex=4;
							break;
						case "Alumni":
							myItem.ImageIndex=5;
							break;
					}
				}
			}
		}
		
		private void RefreshCount()
		{
			if(this.lvwServicePatrollers.Items.Count==1)
			{
				this.lblPatrollerCount.Text = this.lvwServicePatrollers.Items.Count.ToString() + " patroller";
			}
			else
			{
				this.lblPatrollerCount.Text = this.lvwServicePatrollers.Items.Count.ToString() + " patrollers";
			}
		}
		
		void BtnAddClick(object sender, EventArgs e)
		{
			//make sure there is a service type selected...
			if(this.comboBox1.Text=="")
			{
				MessageBox.Show("You need to select a service type.",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			else
			{
				foreach(ListViewItem item in this.lvwPatrollers.SelectedItems)
				{
					ListViewItem svcItem = item.Clone() as ListViewItem;
					this.lvwServicePatrollers.Items.Add(svcItem);
				}
			}
			this.comboBox1.Text="";
			
			RefreshCount();
		}
		
		void BtnResetClick(object sender, EventArgs e)
		{
			this.lvwServicePatrollers.Items.Clear();
			
			RefreshCount();
		}
		
		void BtnRemoveClick(object sender, EventArgs e)
		{
			foreach(ListViewItem item in this.lvwServicePatrollers.SelectedItems)
			{
				item.Remove();
			}
			
			RefreshCount();
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			if(this.lvwServicePatrollers.Items.Count==0)
			{
				MessageBox.Show("You need to select patrollers to assign a service day.",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			else
			{
				
					DialogResult result = MessageBox.Show("Are you sure you want to record " + this.monthCalendar1.SelectionStart.ToShortDateString() + " as a service day for the " + this.lvwServicePatrollers.Items.Count.ToString() + " selected patrollers?",Application.ProductName,MessageBoxButtons.YesNo,MessageBoxIcon.Question);
					if(result==DialogResult.Yes)
					{
						foreach(ListViewItem item in lvwServicePatrollers.Items)
						{
							ButternutSkiPatrol.Person itemPerson = (ButternutSkiPatrol.Person)item.Tag;
							
							
							ButternutSkiPatrol.Person edit = DataEngine.dataFile.roster.Patrollers.Find(delegate(ButternutSkiPatrol.Person addDay)
			                {
								return addDay.LastName==itemPerson.LastName && addDay.FirstName==itemPerson.FirstName;
			                });
							if(edit != null)
							{
								edit.ServiceDays.Add(new ButternutSkiPatrol.ServiceDay(this.monthCalendar1.SelectionStart.ToShortDateString(),this.comboBox1.SelectedItem.ToString()));
							}
						}
						this.lblPatrollerCount.Text="0 patrollers";
						DataEngine.dataFile.ServiceDayRecord=this.monthCalendar1.SelectionStart.ToShortDateString();
						DataEngine.SaveRosterFile();
						this.lvwServicePatrollers.Items.Clear();
					}
				
			}
		}
		
		void UcServiceDaysLoad(object sender, EventArgs e)
		{
			
		}
	}
}
